﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using YouHP.Model;
using YouHP.IDAL;
using Microsoft.ApplicationBlocks.Data;

namespace YouHP.SqlServerDAL
{
    public class NewsCommentDAL:INewsComment
    {
        #region INewsComment 成员

        public bool AddNewsComment(NewsComment info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                        Common.GroupNewsSP_AddNewsComment,
                        info.NewsID,
                        info.UserID,
                        info.Alias,
                        info.CommentData,
                        info.InsertDateTime
                        ) > 0;
        }

        public bool UpdateNewsComment(NewsComment info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                        Common.GroupNewsSP_UpdateNewsComment,
                        info.Id,
                        info.NewsID,
                        info.UserID,
                        info.Alias,
                        info.CommentData,
                        info.InsertDateTime
                        ) > 0;
        }

        public bool DeleteNewsComment(int commentId)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                        Common.GroupNewsSP_DelNewsComment,
                        commentId
                        ) > 0;
        }

        public int GetNewsCommentCount(int newsId)
        {
            SqlDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr, Common.GroupNewsSP_GetNewsCommentCount, newsId);
            int count = -1;
            if (reader != null && reader.Read())
            {
                count = reader.GetInt32(0);
            }
            return count;
        }

        public NewsComment GetNewsComment(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GroupNewsSP_DelNewsComment,
                id) as IDataReader;
            return CBO.FillObject<NewsComment>(reader);
        }

        public List<NewsComment> GetNewsCommentList(int newsId)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.GroupNewsSP_GetNewsCommentList,
                newsId);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                dr.Close();
            }
            else
            {
                
            }
            dr = dset.Tables[0].CreateDataReader();
            return CBO.FillCollection<NewsComment>(dr);
        }

        #endregion
    }
}
